<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #list_list button{line-height: 30px!important;height: 30px!important;}
    #list_list .buttonnone{margin: 5px 20px;color: #707070;background-color: #fff;}
    #list_list .buttonlv{margin: 5px 20px;background-color: #248BFE}
    #list_list .chan{display: none}
    #list_list .shou{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">编辑总价汇总表</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>总价汇总表</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>单位工程名称</th>
                            <th>内容</th>
                            <th><text>*</text>成本价</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 300px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;

    function set_chan(obj) {
        var key = $(obj).parents('tr').index();
        openPage('huishu_set_chan.php?key='+key,'90%','90%');
    }


    var qinglist, otherlist;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_main_huishu_edit,data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                qinglist = res.data.qinglist;
                otherlist = res.data.otherlist;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input info"></td>';
                listAddStr += '<td>';
                listAddStr += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonnone chanbut">';
                listAddStr += '<span>设置</span>';
                listAddStr += '<span class="chan"></span>';
                listAddStr += '</button>';
                listAddStr += '</td>';
                listAddStr += '<td><input class="layui-input explain"></td>';
                short_table_opt_func(listAddStr,1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input info" value="'+list[key].info+'"></td>';
                        str += '<td>';
                        if(list[key].chan){
                            str += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonlv chanbut">';
                        }else{
                            str += '<button type="button" onclick="set_chan(this)" class="layui-btn buttonnone chanbut">';
                        }
                        str += '<span>设置</span>';
                        str += '<span class="chan">'+list[key].chan+'</span>';
                        str += '</button>';
                        str += '</td>';
                        str += '<td><input class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }

                update_refresh_row_func();
                render_from();
            }
        });


        form.on('submit(add)', function(data){

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var info = $(this).find('.info').val();
                    var chan = $(this).find('.chan').text();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('第'+index+'行，请输入单位工程名称');
                    }
                    if(!chan){
                        throw new Error('第'+index+'行，请设置成本价');
                    }


                    list.push({
                        name:name,
                        info:info,
                        chan:chan,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.enginid = param_enginid;
            facade.ajax({url: ask_main_huishu_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });

    });

</script>
</body>
</html>
